/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MasterPesquisa.java
 *
 * Created on 31/12/2012, 15:10:52
 */
package inovacao.gui;

import inovacao.interfaces.MasterForm;
import inovacao.modelos.PagingModel;
import java.awt.Component;
import java.lang.Object;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.RowFilter.ComparisonType;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Giovani
 */
public class MasterFormPesquisa extends JPanel implements MasterForm {

    /** Creates new form MasterPesquisa */
    private JTable jTable;
    private TableRowSorter<PagingModel> sorter;

    public MasterFormPesquisa(JTable jTable) {
        this.jTable = jTable;
        initComponents();
        PagingModel model = (PagingModel) this.jTable.getModel();
        sorter = new TableRowSorter<PagingModel>(model);
        this.jTable.setRowSorter(sorter);
    }

    private void filtroIntervalo() {
        RowFilter<PagingModel, Object> rf = null;
        RowFilter<PagingModel, Object> rf1 = null;
        float interval1 = 0;
        float interval2 = 0;
        if (!jTextFieldIntervalo1.getText().equals("") && !jTextFieldIntervalo2.getText().equals("")) {
            interval1 = Float.parseFloat(jTextFieldIntervalo1.getText()) - 0.001f;
            interval2 = Float.parseFloat(jTextFieldIntervalo2.getText()) + 0.001f;


            try {

                List<RowFilter<PagingModel, Object>> list = new ArrayList<RowFilter<PagingModel, Object>>();
                rf = RowFilter.numberFilter(ComparisonType.AFTER, interval1,4);
                list.add(rf);
                rf = RowFilter.numberFilter(ComparisonType.BEFORE, interval2,4);
                list.add(rf);
                rf1 = RowFilter.andFilter(list);
            } catch (java.util.regex.PatternSyntaxException e) {
                return;
            }
        } else {
            rf1 = null;
        }
        sorter.setRowFilter(rf1);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonSaldoMedio = new javax.swing.JButton();
        jTextFieldSaldoMedio = new javax.swing.JTextField();
        jButtonSaldoTotal = new javax.swing.JButton();
        jTextFieldSaldoTotal = new javax.swing.JTextField();
        jButtonMenorSaldo = new javax.swing.JButton();
        jButtonMaiorSaldo = new javax.swing.JButton();
        jTextFieldIntervalo1 = new javax.swing.JTextField();
        jTextFieldIntervalo2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonSaldoMedio.setText("Saldo devedor médio");
        jButtonSaldoMedio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaldoMedioActionPerformed(evt);
            }
        });

        jButtonSaldoTotal.setText("Saldo devedor total");
        jButtonSaldoTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaldoTotalActionPerformed(evt);
            }
        });

        jButtonMenorSaldo.setText("Menor saldo devedor");
        jButtonMenorSaldo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMenorSaldoActionPerformed(evt);
            }
        });

        jButtonMaiorSaldo.setText("Maior saldo devedor");
        jButtonMaiorSaldo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMaiorSaldoActionPerformed(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("a");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jButton5.setText("Intervalo de saldo");
        jButton5.setActionCommand("Intervalo");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonSaldoTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonSaldoMedio))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldIntervalo1, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldIntervalo2, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                            .addComponent(jTextFieldSaldoTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addComponent(jTextFieldSaldoMedio, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)))
                    .addComponent(jButtonMaiorSaldo, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                    .addComponent(jButtonMenorSaldo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIntervalo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldIntervalo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSaldoMedio)
                    .addComponent(jTextFieldSaldoMedio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSaldoTotal)
                    .addComponent(jTextFieldSaldoTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonMenorSaldo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonMaiorSaldo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
public void saldoMedio()
{
     float saldoMedio = 0;
    for (int i = 0; i < this.jTable.getRowCount(); i++) {
        saldoMedio += Double.parseDouble(String.valueOf(this.jTable.getValueAt(i, 4)));
    }
    if (saldoMedio > 0) {
        saldoMedio = (saldoMedio / this.jTable.getRowCount());
    }
    this.jTextFieldSaldoMedio.setText("" + saldoMedio);
}
private void jButtonSaldoMedioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaldoMedioActionPerformed
// TODO add your handling code here:
    saldoMedio();
}//GEN-LAST:event_jButtonSaldoMedioActionPerformed
private void jButtonMenorSaldoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMenorSaldoActionPerformed
// TODO add your handling code here:

    if (this.jTable.getRowCount() > 0) {
        int saldoMenor = 0;
        double saldoMenorAux = 0;
        saldoMenorAux = Double.parseDouble(String.valueOf(this.jTable.getValueAt(0, 4)));

        for (int i = 0; i < this.jTable.getRowCount(); i++) {

            if (Double.parseDouble(String.valueOf(this.jTable.getValueAt(i, 4))) < saldoMenorAux) {
                saldoMenorAux = Double.parseDouble(String.valueOf(this.jTable.getValueAt(i, 4)));
                saldoMenor = i;
            }
        }
        this.jTable.setColumnSelectionInterval(0, this.jTable.getColumnCount() - 1);
        this.jTable.setRowSelectionInterval(saldoMenor, saldoMenor);
        ((GridDetalhe) this.jTable).scrollToVisible(this.jTable, saldoMenor, saldoMenor);
    }
}//GEN-LAST:event_jButtonMenorSaldoActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    filtroIntervalo();
    saldoMedio();
    saldoTotal();
}//GEN-LAST:event_jButton5ActionPerformed

private void jButtonMaiorSaldoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMaiorSaldoActionPerformed
// TODO add your handling code here:

    if (this.jTable.getRowCount() > 0) {
        int saldoMaior = 0;
        double saldoMaiorAux = 0;

        for (int i = 0; i < this.jTable.getRowCount(); i++) {

            if (Double.parseDouble(String.valueOf(this.jTable.getValueAt(i, 4))) > saldoMaiorAux) {
                saldoMaiorAux = Double.parseDouble(String.valueOf(this.jTable.getValueAt(i, 4)));
                saldoMaior = i;
            }
        }
        this.jTable.setColumnSelectionInterval(0, this.jTable.getColumnCount() - 1);
        this.jTable.setRowSelectionInterval(saldoMaior, saldoMaior);
        ((GridDetalhe) this.jTable).scrollToVisible(this.jTable, saldoMaior, saldoMaior);
    }
}//GEN-LAST:event_jButtonMaiorSaldoActionPerformed
public void saldoTotal()
{
     float saldoMedio = 0;
    for (int i = 0; i < this.jTable.getRowCount(); i++) {
        saldoMedio += Double.parseDouble(String.valueOf(this.jTable.getValueAt(i, 4)));
    }
    this.jTextFieldSaldoTotal.setText("" + saldoMedio);
}
private void jButtonSaldoTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaldoTotalActionPerformed
// TODO add your handling code here:
   saldoTotal();
}//GEN-LAST:event_jButtonSaldoTotalActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButtonMaiorSaldo;
    private javax.swing.JButton jButtonMenorSaldo;
    private javax.swing.JButton jButtonSaldoMedio;
    private javax.swing.JButton jButtonSaldoTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextFieldIntervalo1;
    private javax.swing.JTextField jTextFieldIntervalo2;
    private javax.swing.JTextField jTextFieldSaldoMedio;
    private javax.swing.JTextField jTextFieldSaldoTotal;
    // End of variables declaration//GEN-END:variables

    @Override
    public ArrayList<Component> allComponentes() {
        ArrayList<Component> componentes = new ArrayList<Component>();
        componentes.add(getjTextFieldIntervalo1());
        componentes.add(getjTextFieldIntervalo2());
        componentes.add(getjTextFieldSaldoMedio());
        componentes.add(getjTextFieldSaldoTotal());
        return componentes;
    }

    public JTextField getjTextFieldIntervalo1() {
        return jTextFieldIntervalo1;
    }

    public void setjTextFieldIntervalo1(JTextField jTextFieldIntervalo1) {
        this.jTextFieldIntervalo1 = jTextFieldIntervalo1;
    }

    public JTextField getjTextFieldIntervalo2() {
        return jTextFieldIntervalo2;
    }

    public void setjTextFieldIntervalo2(JTextField jTextFieldIntervalo2) {
        this.jTextFieldIntervalo2 = jTextFieldIntervalo2;
    }

    public JTextField getjTextFieldSaldoMedio() {
        return jTextFieldSaldoMedio;
    }

    public void setjTextFieldSaldoMedio(JTextField jTextFieldSaldoMedio) {
        this.jTextFieldSaldoMedio = jTextFieldSaldoMedio;
    }

    public JTextField getjTextFieldSaldoTotal() {
        return jTextFieldSaldoTotal;
    }

    public void setjTextFieldSaldoTotal(JTextField jTextFieldSaldoTotal) {
        this.jTextFieldSaldoTotal = jTextFieldSaldoTotal;
    }
}
